
package com.bei_en.teacher.management.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.model.system.condition.SystemUserCondition;
import com.bei_en.teacher.management.model.system.condition.SystemUserRoleCondition;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.service.system.SystemLogsService;
import com.bei_en.teacher.management.service.system.SystemResourceService;
import com.bei_en.teacher.management.service.system.SystemUserRoleService;
import com.bei_en.teacher.management.service.system.SystemUserService;
import com.bei_en.teacher.management.shiro.EndecryptUtils;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 *
 * @author Longqs
 * @date 2016-6-15 14:39:11
 */
@Controller
@RequestMapping("/admin")
public class SystemUserController extends BaseController {

    @Autowired
    SystemUserService systemUserService;
    @Autowired
    SystemResourceService systemResourceService;
    @Autowired
    SystemLogsService systemLogsService;
    @Autowired
    SystemUserRoleService systemUserRoleService;

    private void addSharedAttr(Model model) {

	model.addAttribute("roleMap", roleMap());

	model.addAttribute("statusList", gson.fromJson("{1:'正常',0:'删除',2:'锁定'}", Map.class));
    }

    @RequestMapping("/systemUser")
    public String systemUser(Model model, SystemUserCondition vo, HttpServletResponse resp) {

	List<SystemUserVo> list = new ArrayList<SystemUserVo>();
	try {
	    list = systemUserService.findSystemUserPage(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}

	model.addAttribute("list", list);
	addSharedAttr(model);
	model.addAttribute("vo", vo);

	return "/admin/system/userList.html";
    }

    @RequestMapping("/systemUserAdd")
    public String systemUserAdd(Model model, SystemUserVo vo, boolean show, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    addSharedAttr(model);
	    return "/admin/system/userAdd.html";
	}
	try {
	    SystemUserVo user = EndecryptUtils.md5Password(vo.getName(), vo.getPassword(), 2);
	    vo.setPassword(user.getPassword());
	    vo.setSalt(user.getSalt());
	    vo.setStatus(1);
	    vo.setCreateTime(new Date());
	    systemUserService.insert(vo);

	    SystemUserVo obj = getLoginUser(session);
	    String content = "新增管理员" + vo.getName();
	    logsInsert(obj.getId(), content, 1);

	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("systemUserList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/systemUserEdit")
    public String systemUserEdit(Model model, boolean show, SystemUserVo vo, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    SystemUserVo vo1 = systemUserService.findById(vo.getId());

	    SystemUserRoleCondition cvo = new SystemUserRoleCondition();
	    cvo.setUserId(vo.getId());
	    cvo.setPageSize(Integer.MAX_VALUE);
	    cvo.setOrderDirection("asc");
	    cvo.setOrderField("role_id");
	    vo1.setUserRole(systemUserRoleService.findSystemUserRolePageString(cvo));

	    addSharedAttr(model);
	    model.addAttribute("vo", vo1);
	    return "/admin/system/userEdit.html";
	}
	try {
	    vo.setUpdateTime(new Date());
	    systemUserService.updateSelective(vo);

	    SystemUserVo obj = getLoginUser(session);
	    String content = "编辑管理员" + vo.getName();
	    logsInsert(obj.getId(), content, 2);

	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("systemUserList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/systemUserDel")
    @ResponseBody
    public MyJsonResult systemUserDel(Model model, int[] delids, HttpSession session) {

	try {
	    for (int i : delids) {
		SystemUserVo vo1 = systemUserService.findById(i);
		vo1.setStatus(0);
		vo1.setUpdateTime(new Date());
		systemUserService.updateSelective(vo1);

		SystemUserVo obj = getLoginUser(session);
		String content = "删除管理员" + vo1.getName();
		logsInsert(obj.getId(), content, 3);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return new MyJsonFailResult(e.getMessage());
	}
	return new MyJsonSuccessResult();
    }

}
